package com.situ.onlinebookstore.front.controller.api;

import com.situ.onlinebookstore.common.Validation.ValidationType;
import com.situ.onlinebookstore.common.entity.Admin;
import com.situ.onlinebookstore.common.entity.User;
import com.situ.onlinebookstore.common.vo.Result;
import com.situ.onlinebookstore.front.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class UserApiController {
    private final UserService userService;

    //登录
    @PostMapping("/login")
    public Result login(@Validated(ValidationType.Login.class) @RequestBody User user, HttpSession session) {
        //1，获取参数
        //2，验证格式
        try {
            user = userService.login(user);
            //登录成功
            session.setAttribute("user", user);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }



    @PostMapping("/setPayPassword")
    public Result setPayPassword(@Validated(ValidationType.SetPayPassword.class) User user) {
        //必须传递密码 支付密码
        return Result.ok();
    }

    @PostMapping("/rePwd")
    public void rePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1-获取参数
        String oldPwd = request.getParameter("oldPwd");
        String newPwd = request.getParameter("newPwd");
        String rePwd = request.getParameter("rePwd");
        try {
            //2-校验参数 //3-16
            if (oldPwd == null || !oldPwd.matches("^.{3,16}$")) {
                throw new Exception("旧密码格式不正确");
            }
            if (newPwd == null || !newPwd.matches("^.{3,16}$")) {
                throw new Exception("新密码格式不正确");
            }
            if (rePwd == null || !rePwd.equals(newPwd)) {
                throw new Exception("两次密码不一致");
            }

//            获取当前登录的员工,如果员工没登录
            User user = (User) request.getSession().getAttribute("user");
//            3-调用service
            userService.changePassword(oldPwd, newPwd, user.getId());
            //修改成功
            request.getSession().invalidate();
            //返回JSON
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append("{\"code\": 0}");

        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            //返回JSON    失败， 错误信息
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append("{\"code\": 1, \"msg\": \"" + e.getMessage() + "\"}");
        }
    }


}
